package crazyjava;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;

public class ThreadInterview3 {

	public static void main(String[] args) {
		
		final BlockingQueue<String> queen = new ArrayBlockingQueue<String>(10, true);
        final Semaphore sem = new Semaphore(1);
		System.out.println("begin:" + (System.currentTimeMillis() / 1000));
		for (int i = 0; i < 10; i++) { // 这行不能改动
			String input = i + ""; // 这行不能改动
			queen.add(input);
			/*String output = TestDao1.doSome(input);
			System.out.println(Thread.currentThread().getName() + ":" + output);*/
		}
		for(int i=0;i<10;i++){
			new Thread(new Runnable() {
				
				public void run() {
					try {
						sem.acquire();
						String input = queen.take();
						String output = TestDao1.doSome(input);
						System.out.println(Thread.currentThread().getName()+" "+output);
						sem.release();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}).start();
		}
	}
}

// 不能改动此TestDo类
class TestDao1 {
	public static String doSome(String input) {

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String output = input + ":" + (System.currentTimeMillis() / 1000);
		return output;
	}
}
